/**
 *   Copyright (C) 2012  Development Solutions (DS) Software
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   The following additional terms will apply to the software provided with
 *   this file.
 *      1. Preserve the original authors’ name.
 *      2. Documenting all contributions and specify the contributor's name,
 *         the date of the contribution and some email to future contact.
 * 
 */
package org.dss.gil.util.generic;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.dss.gil.util.Globals;
 
/**
 *  This class is to manage the messages and properties
 *  @author  Fernando Gil <lobho.gil@gmail.com>
 *  @since   September 2012
 *  @version 1.0
 */

public class PropertyFileReader {
    private Properties bundle;
    private String filePath;
    private static final Logger logger = Logger.getLogger(PropertyFileReader.class);
 
    private PropertyFileReader(String filePath) throws FileNotFoundException, IOException{
        this.filePath = filePath;
        bundle = new Properties();
        bundle.load(new FileInputStream(filePath));
    }
    
    private PropertyFileReader(InputStream file) throws FileNotFoundException, IOException{
        bundle = new Properties();
        bundle.load(file);
    }
    
    public static PropertyFileReader getPropertyFileReader(InputStream file){
        try {
            PropertyFileReader prop = new PropertyFileReader(file);
            return prop;
        } catch (Exception ex) {
            logger.fatal(ex.getMessage());
            return null;
        }
    }
    
    public static PropertyFileReader getPropertyFileReader(String filePath){
        try {
            PropertyFileReader prop = new PropertyFileReader(filePath);
            return prop;
        } catch (Exception ex) {
            logger.fatal(ex.getMessage());
            return null;
        }
    }
            
    public String getProperty(String key) {
        try {
            return bundle.getProperty(key);
        } catch (MissingResourceException mre) {
            logger.fatal(mre.getMessage());
            return "";
        }
    }
    
    public String getMessage(String key) {
        return getProperty(key);
    }

    public String getFormattedMessage(String key, Object... params) {
        try {
            String message = bundle.getProperty(key);
            if (params == null || params.length == 0) {
                 return message;
            } else {
                 return MessageFormat.format(message, params);
            }
        } catch (Exception e) {
            logger.fatal(e.getMessage());
            return null;
        }
    }

    public void setProperty(String key, String value) {
        bundle.setProperty(key, value);
    }
    
    public void save(String filePath, String fileComments) throws FileNotFoundException, IOException{
        bundle.store(new FileOutputStream(filePath), fileComments);
    }

}
 
